package com.itheima.service.impl;


import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.EmployeeMapper;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

/**
 * @author itheima
 * @since 2022-05-03
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     *
     * @param employee
     * @return
     */
    @Override
    public Boolean addEmployee(Employee employee) {
                employee.setCreateTime(new Date(System.currentTimeMillis()));
                employee.setUpdateTime(employee.getCreateTime());
                employee.setStatus(1);
                employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
                employee.setId(new Snowflake().nextIdStr());
                int insert = employeeMapper.insert(employee);

                return insert > 0;

    }

    @Override
    public Boolean modifyEmployee(Employee employee) {
        if(employee!=null) {

            int update = employeeMapper.update(employee);
            return update>0;
        }

        return false;
    }

    @Override
    public Employee employeeLogin(String username, String password) {

        Employee employee = employeeMapper.login(username, password);

        return employee;
    }

    @Override
    public Page<Employee> getPage(Integer page, Integer pageSize, String name) {
        //组装分页条件
        Integer skip = (page-1)*pageSize;

        List<Employee> employeeList = employeeMapper.selectPage(skip, pageSize, name);
        //查询满足条件的总记录
        Long employeeAmount = employeeMapper.selectTotal(name);

        Page<Employee> pageInfo = new Page<>();
        pageInfo.setRecords(employeeList);
        pageInfo.setTotal(employeeAmount);
       // pageInfo.setTotal(employeeList.size());
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        return pageInfo;
    }

    @Override
    public Employee getById(String id) {

        return employeeMapper.selectById(id);

    }


}
